from twisted.words.protocols.jabber import jid, xmlstream
from twisted.internet import reactor
from twisted.python import log
from wokkel import disco

import common, data
from sqlobject import AND
    
class MasterMucProtocol( common.CommonMucProtocol ):
    pass

class MasterMessageProtocol( common.CommonMessageProtocol ):
    pass

class MasterPresenceProtocol( common.CommonPresenceProtocol ):
    pass
        
class MasterRosterProtocol( common.CommonRosterProtocol ):
    pass
            
class MasterClient( object ):
    
    def __init__( self, user, resource, password, server, port, db_type, db_location ):
        
        self.server = server
        self.port = int( port )
                       
        self.jid = jid.JID( user + '@' + server + "/" + resource )
        self.factory = common.CommonClientFactory( self, password)
        self.factory.streamManager.logTraffic = True
    
        data.init( db_type, db_location, True )
    
        self.muc_client = MasterMucProtocol( self )
        self.muc_client.setHandlerParent( self.factory.streamManager )
        
        self.msg_client = MasterMessageProtocol( self )
        self.msg_client.setHandlerParent( self.factory.streamManager )
    
        self.pre_client = MasterPresenceProtocol( self )
        self.pre_client.setHandlerParent( self.factory.streamManager )
        self.pre_client.available()
    
        self.rtr_client = MasterRosterProtocol( self )
        self.rtr_client.setHandlerParent( self.factory.streamManager )
        
        self.rpc_client = common.RPCProtocol( self )
        self.rpc_client.setHandlerParent( self.factory.streamManager )
        self.rpc_client.subscribeMethod( 'registerClient', self.register_client )
        
        self.dis_client = disco.DiscoClientProtocol()
        self.dis_client.setHandlerParent( self.factory.streamManager )
        
        reactor.connectTCP( self.server, self.port, self.factory )
        
    def register_client(self, iq, client_data):
        print 'register_client'
        
        ip = client_data[0]
        hostname = client_data[1]
        print ip, hostname
        
        host = data.Host.select( AND( data.Host.q.hostname == hostname, data.Host.q.ip == ip ) ).getOne(None)
        
        if not host:
            return common.RPCFault( 1, 'host not found' )
        
        def createRoomResponse( result ):
            print 'createRoomResponse'
            self.muc_client.inviteEntity( jid.JID( iq['from'] ), jid.JID( result['from'] ) )
        
        # add to service room
        for service in list( host.services ):
            print service.name
            self.muc_client.createRoom( service.name ).addCallback( createRoomResponse )
            
            # add to category room
            if service.category:
                self.muc_client.createRoom( service.category.name ).addCallback( createRoomResponse )
            
        # add to location room
        for loc in list( host.locations ):
            print loc.name
            self.muc_client.createRoom( loc.name ).addCallback( createRoomResponse ) 
        
        return True
                
        